﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Metro;
using ProductManagement.Ultilities;

namespace ProductManagement.Presentation_Layer
{
    public partial class EditForm : MetroForm
    {
        #region Variables

        private int _id;
        private ProductManagementClassesDataContext db = new ProductManagementClassesDataContext();
        private Validator validator = new Validator();

        #endregion

        #region Constructors

        public EditForm(int id)
        {
            _id = id;
            InitializeComponent();
        }

        #endregion

        #region Methods

        private bool ValidateEditData()
        {
            bool isValidName = validator.ValidateName(txtEditName, errorProvider1);
            bool isValidPrice = validator.ValidatePrice(txtEditPrice, errorProvider1);
            bool isValidQuantity = validator.ValidateQuantity(txtEditQuantity, errorProvider1);
            bool isValidYear = validator.ValidateYear(txtEditYear, errorProvider1);
            return (isValidName && isValidPrice && isValidQuantity && isValidYear);
        }

        private void DisplayProductInfomation()
        {
            var product = db.sp_GetProductInfomation(_id).SingleOrDefault();
            txtEditName.Text = product.name;
            txtEditPrice.Text = product.price.ToString();
            txtEditQuantity.Text = product.quantity.ToString();
            txtEditYear.Text = product.year.ToString();
        }

        #endregion

        #region Events handling

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (ValidateEditData())
            {
                DialogResult dr = MessageBoxEx.Show("Bạn có muốn sửa thông tin sản phẩm?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    string name = txtEditName.Text;
                    decimal price = Decimal.Parse(txtEditPrice.Text);
                    int quantity = Int32.Parse(txtEditQuantity.Text);
                    int year = Int32.Parse(txtEditYear.Text);
                    string state = (quantity > 0) ? "Còn hàng" : "Hết hàng";
                    db.sp_UpdateProduct(_id, name, price, quantity, year, state);
                    MessageBoxEx.Show("Sửa thông tin sản phẩm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void btnResetEdit_Click(object sender, EventArgs e)
        {
            DisplayProductInfomation();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void EditForm_Load(object sender, EventArgs e)
        {
            DisplayProductInfomation();
        }

        #endregion
    }
}
